package com.example.backend_422.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.backend_422.common.Result;
import com.example.backend_422.common.Utils;
import com.example.backend_422.entity.Order;
import com.example.backend_422.mapper.OrderMapper;
import com.example.backend_422.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwyzsn
 * @since 2023-04-22
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {



    @Autowired
    private OrderMapper orderMapper; // 自动装配, 通过 @Autowired 自动装配 OrderMapper

    @Autowired
    private Utils utils; // 自动装配, 通过 @Autowired 自动装配 Utils

    /**
     * 预约时间
     * @param orderRecord
     * @return
     */
    public Result preserveTime(com.example.backend_422.records.Order orderRecord) {
        // 构造订单信息
        Order order = new Order();
        order.setHouseId(orderRecord.houseId());
        order.setHouseName(orderRecord.houseName());
        order.setUserId(orderRecord.userId());
        order.setCreatedTime(utils.stringToDate(orderRecord.createdTime()));
        order.setStatus(orderRecord.status());
        order.setUserPhone(orderRecord.userPhone());
        order.setUsername(orderRecord.username());
        order.setLandlordId(orderRecord.landlordId());
        order.setLandlordPhone(orderRecord.landlordPhone());
        order.setAddress(orderRecord.address());
        order.setLandlordName(orderRecord.landlordName());
        // 插入订单信息
        orderMapper.insert(order);
        return Result.succ("null");
    }

    /**
     * 获取所有订单信息
     * @return
     */
    public Result getByUserId(String userId) {

        QueryWrapper<Order> wrapper = new QueryWrapper<>();// 构造查询条件
        wrapper.eq("user_id",userId); // 查询条件 user_id = userId
        List<Order> orders = orderMapper.selectList(wrapper); // 查询
        return Result.succ(orders);
    }

    /**
     * 根据订单id删除订单信息
     * @param OrderId
     * @return
     */
    public Result cancelByOrderId(Integer OrderId){
        QueryWrapper<Order> wrapper = new QueryWrapper<>(); // 构造查询条件
        wrapper.eq("order_id",OrderId); // 查询条件 order_id = OrderId
        orderMapper.delete(wrapper); // 删除
        return Result.succ("success");
    }

    /**
     * 根据房东id获取订单信息
     * @param landlordId
     * @return
     */
    public Result getByLandlordId(Integer landlordId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>(); // 构造查询条件
        wrapper.eq("landlord_id",landlordId); // 查询条件 landlord_id = landlordId
        List<Order> orders = orderMapper.selectList(wrapper); // 查询
        return Result.succ(orders);
    }

    /**
     * 根据订单id更新订单状态
     * @param orderId
     * @param status
     * @return
     */
    public Result updateStatus(Integer orderId,String status) {
        // 构造更新条件
        UpdateWrapper<Order> wrapper = new UpdateWrapper<>();
        wrapper.eq("order_id",orderId); // 更新条件 order_id = orderId
        wrapper.set("status",status); // 更新字段 status = status
        orderMapper.update(null,wrapper);// 更新
        return Result.succ("success");
    }
}
